﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using ComLib.ConfigService;
using LogLib;

namespace IDCardReaderCZ
{
    public class IDCReaderOption : IniConfigBase
    {
        public override string ConfigDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        public override string ConfigFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_strConfigPath))
                {
                    string className = this.GetType().Name;
                    _strConfigPath = Path.Combine(ConfigDirectory, className + ".ini");
                }
                return _strConfigPath;
            }
        }


        /// <summary>
        /// 连接串口号
        /// </summary>
        private string _readerName = "";
        public string ReaderName
        {
            get { return _readerName; }
            set { _readerName = value; }
        }

        /// <summary>
        /// 是否读取身份证头像
        /// </summary>
        private bool _bReadHeadImage = false;
        public bool ReadHeadImage
        {
            get { return _bReadHeadImage; }
            set { _bReadHeadImage = value; }
        }

        /// <summary>
        /// 是否读取指纹信息
        /// </summary>
        private bool _bReadFingerprint = false;
        public bool ReadFingerprint
        {
            get { return _bReadFingerprint; }
            set { _bReadFingerprint = value; }
        }

        /// <summary>
        /// 是否扫描身份证
        /// </summary>
        private bool _bScanSfz = false;
        public bool ScanSfz
        {
            get { return _bScanSfz; }
            set { _bScanSfz = value; }
        }

        /// <summary>
        /// 阅读器主动扫描间隔时间（毫秒）
        /// </summary>
        private int _scanInterval = 1200;
        public int ScanInterval
        {
            get { return _scanInterval; }
            set { _scanInterval = value; }
        }

        /// <summary>
        /// 连续扫描不成功的超时时间（秒）
        /// </summary>
        private int _timeoutSec = 120;
        public int TimeoutSecond
        {
            get { return _timeoutSec; }
            set { _timeoutSec = value; }
        }

        /// <summary>
        /// 字符串编码名称
        /// </summary>
        private string _encodingName = "UTF-8";
        public string EncodingName
        {
            get { return _encodingName; }
            set { _encodingName = value; }
        }

        public override bool Save()
        {
            base.Save();
            SaveField("ReaderName", _readerName);
            SaveField("ScanInterval", _scanInterval);
            SaveField("TimeoutSecond", _timeoutSec);
            SaveField("ReadHeadImage", _bReadHeadImage);
            SaveField("ScanSfz", _bScanSfz);
            SaveField("ReadFingerprint", _bReadFingerprint);
            SaveField("Encoding", EncodingName);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _readerName = LoadStringValue("ReaderName");
                _scanInterval = LoadInt32Value("ScanInterval");
                _bReadHeadImage = LoadBoolenValue("ReadHeadImage");
                _bReadFingerprint = LoadBoolenValue("ReadFingerprint");
                _bScanSfz = LoadBoolenValue("ScanSfz");
                _encodingName = LoadStringValue("Encoding");
                _timeoutSec = LoadInt32Value("TimeoutSecond");
            }
            catch (Exception ex)
            {
                string strMsg = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("刷身份证选项: ");
            builder.Append("[连接串口号]:" + _readerName.ToString());
            builder.Append("; [阅读器主动扫描间隔时间]:" + _scanInterval);
            builder.Append("; [连续扫描不成功的超时时间（秒）]:" + _timeoutSec);
            builder.Append("; [字符串编码名称]:" + _encodingName);
            builder.Append("; [是否读取身份证头像]:" + (_bReadHeadImage ? "是" : "否"));
            builder.Append("; [是否读取指纹信息]:" + (_bReadFingerprint ? "是" : "否"));
            builder.Append("; [是否扫描身份证]:" + (_bScanSfz ? "是" : "否"));
            return builder.ToString();
        }

    }
}
